<?php
class newsControl extends rbacControl {

    function index() {
        $db = K('newsview');
        $catid = isset($_GET['catid']) ? $_GET['catid'] : false;
        $where_catid = $catid ? "catid = '$catid'" : '';
        $where_status = isset($_GET['status']) ? "status='{$_GET['status']}'" : '';
        $count = $db->where($where_catid)->where($where_status)->where("recycled=0")->count();
        $page = new page($count, 5);
        $news = $db->field("id,title,click,username,status,news.catid,cname,addtime")->where($where_catid)->where($where_status)->where("recycled=0")->limit($page->limit())->all();
		//p($news);
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
    }
	
	//审核
	function status(){
	$id=$_GET['id'];
		$db=M("news");
	if($db->save(array("id"=>$id,"status"=>1))){
				$this->success("审核文章成功！");
			}else{
				$this->error("审核文章失败！");
			};
	
	}
	//添加文章视图
    function add_show() {
        if (!isset($_GET['catid'])) {
            $catdb = M("category");
            $category = $catdb->field("catid,cname,pid")->all();
            $category = arr::channel($category, 'catid', 'pid', 0, 2);
            $this->assign("category", $category);
        }
        $this->display();
    }

    function add() {
        $db = K("newsrelation");
		$_POST['news_data']['userip'] = ip_get_client();
		$_POST['addtime'] = time();
		$_POST['updatetime'] = time();
		$_POST['thumb']=isset($_POST['thumb'][1][0])?$_POST['thumb'][1][0]:'';
        if ($db->add()) {
            $this->success("文章添加成功!  <a href='#'>返回栏目页</a>|<a href='#'>查看文章</a>|<a href='#'>继续添加文章</a>", '', 3);
        } else {
            $this->error("文章添加失败");
        }
    }
	//编辑文章显示
	function edit_show(){
		$id=$_GET["id"];
		$db = K('newsview');
		
		if (!isset($_GET['catid'])) {
            $catdb = M("category");
            $category = $catdb->field("catid,cname,pid")->all();
            $category = arr::channel($category, 'catid', 'pid', 0, 2);
            $this->assign("category", $category);
        }
		//p($category);
		$news = $db->join("news_data")->where("id=$id")->field("id,nid,title,news.keywords,description,gourl,body,source,template")->all();
		//p($news);
		
        $this->assign("news", $news);
     
        $this->display();
	}
	//编辑文章显示
	function edit(){
	
		$db = K("newsrelation");
		$_POST['news_data']['userip'] = ip_get_client();
		$_POST['addtime'] = time();
		$_POST['updatetime'] = time();
		$_POST['thumb']=isset($_POST['thumb'][1][0])?$_POST['thumb'][1][0]:'';
        if ($db->save()) {
            $this->success("文章修改成功!  <a href='#'>返回栏目页</a>|<a href='#'>查看文章</a>|<a href='#'>继续添加文章</a>", '', 3);
        } else {
            $this->error("文章修改失败");
        }
	
	}
	
    //删除文章
    function del() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 1))) {
            $this->success("文件已放入回收站");
        } else {
            $this->error("删除文章失败");
        }
    }

    //彻底删除文章
    function del_news() {
        $id = $_GET['id'];
        $news_db = K("newsrelation");
        if ($news_db->del($id)) {
            $this->success("删除成功");
        } else {
            $this->del("删除文章失败"
            );
        }
    }

    //回收站
    function recycled() {
        $db = K('newsview');
        $count = $db->where("recycled=1")->count();
        $page = new page($count, 2);
        $news = $db->field("id,title,click,username,status,news.catid,cname,addtime")->
                        where("recycled=1")->limit($page->limit())->all();
        $this->assign("news", $news);
        $this->assign("page", $page->show(2));
        $this->display();
    }

    //还原
    function restore() {
        $id = $_GET['id'];
        $db = M("news");
        if ($db->save(array("id" => $id, "recycled" => 0))) {
            $this->success("文章已还原");
        } else {
             $this->error("还原文章失败，请重试");
        }
    }
	//文章评论列表
	function comment(){
		
		$db=K("commentview");
		$count = $db->count();
        $page = new page($count, 5);
		$news=$db->all();
		p($news);
	
		$this->assign("news",$news);
		$this->assign("row",$row);
		$this->assign("page", $page->show(2));
		$this->display();
	
	}

}

?>
